<?php
/**
 * 测评题目
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\weixin\controllers;

use app\libs\Method;
use app\libs\WeixinControl;
use app\modules\weixin\models\LowProduct;
use app\modules\weixin\models\LowProductChat;
use app\modules\weixin\models\LowProductClass;
use app\modules\weixin\models\LowProductClassRegistration;
use app\modules\weixin\models\LowProductClassYuyue;
use app\modules\weixin\models\LowProductPlayChat;
use app\modules\weixin\models\MemberOpenid;
use app\modules\weixin\models\User;
use yii;


class PlayController extends WeixinControl
{

    function init (){
        parent::init();
        if(!$this->uid){
            die(Method::jsonGenerate(99));
        }
    }

    public $enableCsrfValidation = false;

    /**
     * @Title  [列表]
     * @Author Don
     * @Date   2021-05-11
     * @return [type]
     */
    public function actionIndex()
    {
        $id = Yii::$app->request->post('id');
        $class = LowProductClass::findOne($id);
        $sign = LowProductClassRegistration::find()->where("productId=$class->productId AND FIND_IN_SET('$class->section',section) AND uid=$this->uid AND status=1")->one();
        if(!$sign){
            die(Method::jsonGenerate(0,null,'还未购课'));
        }
        $playData = Method::getPlayUrl(86400);
        $chat = LowProductPlayChat::find()->where("productId=$class->productId")->orderBy('createTime DESC')->one();
        die(Method::jsonGenerate(1,['playData' => $playData,'chatId' => $chat?$chat->id:0,'productId' => $class->productId]));
    }




    public function actionChat()
    {
        $productId = Yii::$app->request->post('productId');
        $content = Yii::$app->request->post('content');
        $user = User::find()->where("uid=$this->uid")->one();
        $model = new LowProductPlayChat();
        $model->content = $content;
        $model->createTime = time();
        $model->nickname = $user->nickname;
        $model->productId = $productId;
        $model->uid = $this->uid;
        $model->save();
        $chatId = $model->primaryKey;
        die(Method::jsonGenerate(1,['chatId' => $chatId]));
    }


    public function actionChatList(){
        $chatId = Yii::$app->request->post('chatId');
        $productId = Yii::$app->request->post('productId');
        $chat = LowProductPlayChat::find()->where("id>$chatId AND productId=$productId")->orderBy('createTime ASC')->asArray()->all();
        $count = count($chat);
        if($count>0){
            $chatId = $chat[$count-1]['id'];
        }else{
            $chatId = null;
        }
        die(Method::jsonGenerate(1,['chat' => $chat,'chatId' => $chatId]));
    }

}